import 'babel-polyfill';
import Vue from 'vue';
import VueRouter from 'vue-router';
import routes from './router/index';
import store from './store/';
import {routerMode} from './config/env';
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css'// progress bar style
import VueYoutube from 'vue-youtube'
import {getToken, setToken, removeToken} from '@/utils/auth'

Vue.use(VueYoutube)

if ("prod" == process.env.ENV_CONFIG) {
    console.log("#######")
    window.sessionStore = window.sessionStorage
} else {

    window.sessionStore = window.localStorage
}

NProgress.configure({showSpinner: false})// NProgress Configuration
import './config/rem';
import FastClick from 'fastclick';

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import './mock/index'


if ('addEventListener' in document) {
    document.addEventListener('DOMContentLoaded', function () {
        FastClick.attach(document.body);
    }, false);
}


import VueVideoPlayer from 'vue-video-player'

// require videojs style
import 'video.js/dist/video-js.css'
import "vue-video-player/src/custom-theme.css"
// import 'vue-video-player/src/custom-theme.css'

Vue.use(VueVideoPlayer, /* {
  options: global default options,
  events: global videojs events
} */)

const whiteList = ['/home', '/authredirect', '/login', '/p1', '/p2', '/p3', '/p4', '/p5', '/p6', '/p7', '/p8', '/p9']// no redirect whitelist


Vue.use(VueRouter)
// 引入element-ui
Vue.use(ElementUI)

import './errorLog'// error log

// import './permission' // permission control

const router = new VueRouter({
    routes,
    mode: routerMode,
    strict: process.env.NODE_ENV !== 'production',
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            if (from.meta.keepAlive) {
                from.meta.savedPosition = document.body.scrollTop;
            }
            return {x: 0, y: to.meta.savedPosition || 0}
        }
    }
})


// 滚动条控制的部分
router.afterEach((to, from) => {
    /*store.commit(type.SAVE_POSITION, {
        name: from.path,
        position: document.body.scrollTop || document.documentElement.scrollTop
    })
    if (store.state.positions[to.path]) {
        setTimeout(() => {
            window.scrollTo(0, store.state.positions[to.path])
        }, 80)
    } else {
        setTimeout(() => {
            window.scrollTo(0, 0)
        }, 80)
    }*/
})


router.beforeEach((to, from, next) => {
    NProgress.start() // start progress bar

    console.log(to.path)

    if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单，直接进入
        next()
        NProgress.done()
    }

    if (store.state.user.status) {
        next()
        NProgress.done()
    } else {

        if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单，直接进入
            next()
        } else {
            var token = window.localStorage.getItem("token") || getToken()
            if ("" == token) {
                window.sessionStore.clear()
                next('/home') // 否则全部重定向到登录页
                store.commit("EMPTY_USER")
                alert("you need to login in first, please refresh page!")
            } else {
                next()
            }
            /*setTimeout(function () {
                window.location.reload()
            }, 1000)*/
            // window.location.reload()
            NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
        }


    }
})

new Vue({
    router,
    store,
}).$mount('#app')
